package com.bjpowernode.crm.advice;

import com.bjpowernode.crm.constants.Constants;
import com.bjpowernode.crm.pojo.User;
import com.bjpowernode.crm.utils.UUIDUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;

@Component
@Aspect
public class CreateAdvice implements Advice {

    @Autowired(required=false)
    private HttpSession session;

    @Around("execution(* *..service..*.save*(..))")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        Object result = null;

        Object[] args = pjp.getArgs(); // 方法的参数

        try {
            User user = (User) session.getAttribute(Constants.LOGIN_USER);
            String createBy = user.getLoginAct() + "|" + user.getName();
            String createTime = Constants.DateFormat.SDF19.format(new Date());

            Object o = args[0]; // 要添加的实体

            if (o instanceof List) {
                List list = (List) o;
                if (list != null && list.size() > 0) {
                    for (Object _o : list) {
                        // 使用反射为对象中的属性赋值
                        Class clazz = _o.getClass();
                        // 通过反射机制绕过封装强行为字段赋值
                        //Field idField = clazz.getDeclaredField("id");
                        //idField.setAccessible(true);
                        //idField.set(o, id);

                        // 通过set方法赋值
                        Method setId = clazz.getMethod("setId", String.class);
                        setId.invoke(_o, UUIDUtils.getUUID());
                        Method setCreateBy = clazz.getMethod("setCreateBy", String.class);
                        setCreateBy.invoke(_o, createBy);
                        Method setCreateTime = clazz.getMethod("setCreateTime", String.class);
                        setCreateTime.invoke(_o, createTime);
                    }
                }
            } else {
                // 使用反射为对象中的属性赋值
                Class clazz = o.getClass();
                // 通过反射机制绕过封装强行为字段赋值
                //Field idField = clazz.getDeclaredField("id");
                //idField.setAccessible(true);
                //idField.set(o, id);

                // 通过set方法赋值
                Method setId = clazz.getMethod("setId", String.class);
                setId.invoke(o, UUIDUtils.getUUID());
                try {
                    Method setCreateBy = clazz.getMethod("setCreateBy", String.class);
                    setCreateBy.invoke(o, createBy);
                } catch (Exception e) {
                }

                try {
                    Method setCreateTime = clazz.getMethod("setCreateTime", String.class);
                    setCreateTime.invoke(o, createTime);
                } catch (Exception e) {
                }

                try {
                    Method setNotePerson = clazz.getMethod("setNotePerson", String.class);
                    setNotePerson.invoke(o, createBy);
                } catch (Exception e) {
                }

                try {
                    Method setNoteTime = clazz.getMethod("setNoteTime", String.class);
                    setNoteTime.invoke(o, createTime);
                } catch (Exception e) {
                }

            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("设置属性失败，业务方法继续执行");
        }

        result = pjp.proceed(); // 调用目标（service）方法
        return result;
    }
}
